package com.jason.kotlin.meta.architecture2.ui.fragments

import android.view.View
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.jason.kotlin.meta.R
import com.jason.kotlin.meta.architecture2.base.WanBaseFragment
import com.jason.kotlin.meta.architecture2.model.ArticleBean
import com.jason.kotlin.meta.architecture2.ui.adapter.HomeArticleAdapter
import com.jason.kotlin.meta.architecture2.viewmodel.SquareViewModel
import com.jason.library.common.base.Status
import com.jason.library.common.ext.observe
import com.jason.library.common.ext.showToast


class SquareFragment : WanBaseFragment() {


    private lateinit var refreshLayout: SwipeRefreshLayout
    private lateinit var recyclerView: RecyclerView

    private lateinit var articleAdapter: HomeArticleAdapter

    private lateinit var viewModel: SquareViewModel

    override fun getLayoutId() = R.layout.fragment_square


    override fun onViewInit(view: View) {
        refreshLayout = view.findViewById(R.id.swipeRefreshLayout)
        recyclerView = view.findViewById(R.id.recyclerView)

        articleAdapter = HomeArticleAdapter(R.layout.item_article)

        articleAdapter.loadMoreModule.setOnLoadMoreListener {
            loadMoreData()
        }
        articleAdapter.loadMoreModule.isEnableLoadMore = false
        articleAdapter.loadMoreModule.isAutoLoadMore = true
        articleAdapter.loadMoreModule.isEnableLoadMoreIfNotFullPage = false

        recyclerView.apply {
            layoutManager = LinearLayoutManager(context)
            setHasFixedSize(true)
            adapter = articleAdapter
        }

        refreshLayout.setOnRefreshListener {
            loadData()
        }
    }

    override fun onDataInit() {
        viewModel =
            ViewModelProvider.AndroidViewModelFactory.getInstance(activity!!.application).create(
                SquareViewModel::class.java
            )
        //数据监听
        bindLiveData()

        loadData()
    }


    private fun loadData() {
        articleAdapter.loadMoreModule.isEnableLoadMore = false
        refreshLayout.isRefreshing = true
        viewModel.loadData()
    }

    private fun loadMoreData() {
        articleAdapter.loadMoreModule.isEnableLoadMore = false
        refreshLayout.isRefreshing = true
        viewModel.loadMoreData()
    }

    /**
     * 绑定LiveData的监听
     */
    private fun bindLiveData() {

        observe(viewModel.pageStatusData) {
            when (it.status) {
                Status.SUCCESS -> {
                    refreshLayout.isRefreshing = false
                    articleAdapter.loadMoreModule.isEnableLoadMore = true
                }
                Status.EMPTY -> {showToast("加载不到数据")
                    refreshLayout.isRefreshing = false}
                Status.ERROR -> {showToast("加载失败")
                    refreshLayout.isRefreshing = false}
                Status.LOADING -> refreshLayout.isRefreshing = true
            }
        }

        //文章 数据监听
        observe(viewModel.articleData) {
            if (it.page > 0) {
                articleAdapter.setNewData(it.data as MutableList<ArticleBean>?)
            } else {
                articleAdapter.addData(it.data!!)
            }


        }


    }
}